package com.ruoyi.web.controller.mes;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.service.Impl.MesPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.mes.MesPaper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 纸张管理Controller
 *
 * @author levicash
 * @date 2024-01-09
 */
@Api("纸张管理")
@RestController
@RequestMapping("/mes/paper")
public class MesPaperController extends BaseController {
    @Autowired
    private MesPaperService mesPaperService;
    @Autowired
    private MesPaperService mesPaperServiceImpl;

    /**
     * 查询纸张管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MesPaper mesPaper) {
        startPage();
        List<MesPaper> list = mesPaperService.selectMesPaperList(mesPaper);
        return getDataTable(list);
    }
    /**
     * 查询裁切组纸张
     */
    @GetMapping("/cuttingGrouplist")
    public TableDataInfo cuttingGrouplist(MesPaper mesPaper) {
        startPage();
        List<MesPaper> list = mesPaperService.cuttingGrouplist(mesPaper);
        return getDataTable(list);
    }

    /**
     * 导出纸张管理列表
     */
    @Log(title = "纸张管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesPaper mesPaper) {
        List<MesPaper> list = mesPaperService.selectMesPaperList(mesPaper);
        ExcelUtil<MesPaper> util = new ExcelUtil<MesPaper>(MesPaper.class);
        util.exportExcel(response, list, "纸张管理数据");
    }

    /**
     * 导入客户信息数据
     */
    @Log(title = "纸张管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesPaper> util = new ExcelUtil<MesPaper>(MesPaper.class);
        List<MesPaper> mesPaperList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = mesPaperService.importPaper(mesPaperList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载客户信息模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MesPaper> util = new ExcelUtil<MesPaper>(MesPaper.class);
        util.importTemplateExcel(response, "纸张管理数据");
    }

    /**
     * 获取纸张管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mesPaperService.selectMesPaperById(id));
    }

    /**
     * 新增纸张管理
     */
    @Log(title = "纸张管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesPaper mesPaper) {
        return toAjax(mesPaperService.insertMesPaper(mesPaper));
    }

    /**
     * 修改纸张管理
     */
    @Log(title = "纸张管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesPaper mesPaper) {
        MesPaper mode = mesPaperService.selectMesPaperById(mesPaper.getId());
        if (mode.getStatus() == 0) {
            return AjaxResult.error("请先停用后再修改");
        }
        return toAjax(mesPaperService.updateMesPaper(mesPaper));
    }

    /**
     * 删除纸张管理
     */
    @Log(title = "纸张管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mesPaperService.deleteMesPaperByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "纸张管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MesPaper mesPaper) {
        return toAjax(mesPaperService.updateMesPaperStatus(mesPaper));
    }


    @ApiOperation("机台纸张查询")
    @PostMapping("/machinelist")
    public TableDataInfo machinelist(@RequestBody Map<String,Object> params) {
        startPage();
        MesPaper data=new MesPaper();
        data.setParams(params);
        List<MesPaper> list= mesPaperService.MachinePaperlist(data);
        return getDataTable(list);
    }

    @ApiOperation("纸张流转查询")
    @GetMapping("/getPaperCirculationList")
    public TableDataInfo getPaperCirculationList(MesPaper mesPaper){
        startPage();
        List<MesPaper> list= mesPaperService.getPaperCirculationList(mesPaper);
        return getDataTable(list);
    }

    @ApiOperation("纸张流转列表导出")
    @GetMapping("/exportPaperCirculationList")
    public AjaxResult exportPaperCirculationList(MesPaper mesPaper){
        return mesPaperServiceImpl.exportPaperCirculationList(mesPaper);
    }

}
